package nthu.web.model.domain;

import java.io.Serializable;
import nthu.web.model.domain.define.LocationInterface;

@SuppressWarnings("serial")
public class Location implements LocationInterface,Serializable { 
    private String name;
    private Coordinate coordinate;   
   

    public Location(String name, Coordinate coordinate) {
        this.name = name;
    	this.coordinate = coordinate;
    }

    // return distance between this location and that location
	public double distanceTo(Location that) {
        double STATUTE_MILES_PER_NAUTICAL_MILE = 1.15077945;
        double lat1 = Math.toRadians(coordinate.getLatitude());
        double lon1 = Math.toRadians(coordinate.getLongitude());
        double lat2 = Math.toRadians(that.coordinate.getLatitude());
        double lon2 = Math.toRadians(that.coordinate.getLongitude());

        // great circle distance in radians, using law of cosines formula
        double angle = Math.acos(Math.sin(lat1) * Math.sin(lat2)
                               + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));

        // each degree on a great circle of Earth is 60 nautical miles
        double nauticalMiles = 60 * Math.toDegrees(angle);
        double statuteMiles = STATUTE_MILES_PER_NAUTICAL_MILE * nauticalMiles;
        statuteMiles = statuteMiles * 1.609344; 
        // miles to kilometer 
        return statuteMiles;
    }

    // return string representation of this point
	public String toString() {
        return name + "[緯度,經度]: (" + coordinate.getLongitude() + ", " + coordinate.getLatitude() + ")";
    }


    // just for testing in eclipse
    public static void main(String[] args) {
        Location loc1 = new Location("總圖四樓",new Coordinate(24.7945393, 120.9934832));
        Location loc2 = new Location("資電館326",new Coordinate(24.7948113, 120.9923032));  
        double distance = loc1.distanceTo(loc2);

        System.out.println(loc1 + " 到 " + loc2);
        System.out.printf("距離 %6.3f 公里 \n", distance);
    }
}
